package dao.Impl;

import dao.BaseDao;
import dao.StudentDao;
import entity.Student;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao implements StudentDao {
    Student student=null;
    @Override
    public List<Student> getAll() {
        String sql="SELECT id,name,address,sex FROM student";
        ResultSet rs=executeQuery(sql);
        List<Student> list=new ArrayList<>();
        try {
            while (rs.next()){
                student=new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setAddress(rs.getString("address"));
                student.setSex(rs.getString("sex"));
                list.add(student);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,pstmt,rs);
        }
        return list;
    }

    public int add(String name,String address,String sex){
        String sql="insert into student(name,address,sex) values(?,?,?)";
        Object params[]={name,address,sex};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

    @Override
    public int delete(int id) {
        String sql="delete from student where id=?";
        Object params[]={id};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

    @Override
    public int update(int id,String name,String address,String sex) {
        String sql="update student set name=?,address=?,sex=? where id=?";
        Object params[]={name,address,sex,id};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

}
